package com.yaoxiaowen.project2.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.yaoxiaowen.project2.MainApplication;

/**
 * YaoWen(43194) create at tongcheng work pc,
 * time:  2017/1/24 15:39  qq:2669932513
 *
 * 摘抄自 有票儿 app
 */
public class Utils_App {
    /**
     * 获取版本名
     */
    public static String getVersionName() {
        PackageInfo packageInfo = getPackageInfo();
        if (packageInfo != null) {
            return packageInfo.versionName;
        } else
            return "";
    }

    /**
     * 获取版本号
     */
    public static int getVersionCode() {
        PackageInfo packageInfo = getPackageInfo();
        if (packageInfo != null) {
            return packageInfo.versionCode;
        } else
            return 0;
    }

    //得到 包名
    public static String getPackageName(){
        Context context = MainApplication.getInstance();
        return context.getPackageName();
    }

    /**
     * 获取 渠道名字
     * @return
     */
    public static String getChannelName(){
        Context context = MainApplication.getInstance();

        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            String channel = bundle.getString("CHANNEL");
            return channel;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }


//    /**
//     * 测试  https://github.com/mcxiaoke/packer-ng-plugin  的打包方式
//     * @param context
//     * @return
//     */
//    public static String getPackerMarket(Context context){
//        final String market = PackerNg.getMarket(context);
//        return market;
//    }


    public static int getTargetSdkVersion(){
        int targetSdkVersion;
        try {
            targetSdkVersion = getPackageInfo().applicationInfo.targetSdkVersion;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
        return targetSdkVersion;
    }

    private static PackageInfo getPackageInfo() {
        Context context = MainApplication.getInstance();
        PackageManager manager = context.getPackageManager();
        try {
            return manager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }


}
